package com.msmall.community.wish.service.impl;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.msmall.base.pager.PaginationSupport;
import com.msmall.base.service.impl.BaseManagerImpl;
import com.msmall.base.util.ConstantDB;
import com.msmall.community.common.util.FilterCharsUtil;
import com.msmall.community.common.util.WishUtil;
import com.msmall.community.user.service.UserManager;
import com.msmall.community.wish.dao.WishDao;
import com.msmall.community.wish.model.Wish;
import com.msmall.community.wish.model.WishReply;
import com.msmall.community.wish.model.WishType;
import com.msmall.community.wish.service.WishManager;

public class WishManagerImpl extends BaseManagerImpl implements WishManager{
	private static final Log log = LogFactory.getLog(WishManagerImpl.class);
	private WishDao wishDao;
	
	private UserManager userManager;

	
	public void setWishDao(WishDao wishDao) {
		this.wishDao = wishDao;
	}

	public void setUserManager(UserManager userManager) {
		this.userManager = userManager;
	}
	
	public WishType saveWishType(WishType type, String uat, File bg, String bgFileName, File common, String commonFileName, File active, String activeFileName){
		WishType old = null;
		try {//重名类型
			old = wishDao.loadByName(type.getName());
			if(old != null){
				if(old.getTypeid().compareTo(type.getTypeid()) != 0){
					type.setName(type.getName() + "-1");
				}
			}
		} catch (Exception e) {
			log.error(e);
			return null;
		}
		try{
			if(type.getTypeid() != null && type.getTypeid().longValue() != 0l){
				old = (WishType) wishDao.get(WishType.class, type.getTypeid());
				type.setBgImg(old.getBgImg());
				type.setBgImgName(old.getBgImgName());
				type.setCommonImg(old.getCommonImg());
				type.setCommonImgName(old.getCommonImgName());
				type.setActiveImg(old.getActiveImg());
				type.setActiveImgName(old.getActiveImgName());
			}
		} catch (Exception e) {
			log.error(e);
			return null;
		}
		
		try{
			type = (WishType) wishDao.saveObject(type);
			if(bg != null){
				String[] bgpath = WishUtil.saveBgImage(bg, bgFileName, type.getBgImgName(),  type.getTypeid(), uat);
				type.setBgImg(bgpath[0]);
				type.setBgImgName(bgpath[1]);
			}
			if(common != null){
				String[] commonpath = WishUtil.saveCommonImage(common, commonFileName, type.getCommonImgName(),type.getTypeid(), uat);
				type.setCommonImg(commonpath[0]);
				type.setCommonImgName(commonpath[1]);
			}
			if(active != null){
				String[] activepath = WishUtil.saveActiveImage(active, activeFileName, type.getActiveImgName(), type.getTypeid(), uat);
				type.setActiveImg(activepath[0]);
				type.setActiveImgName(activepath[1]);
			}
			type = (WishType) wishDao.saveObject(type);
			return type;
		}catch(Exception e){
			log.error(e);
			return null;
		}
	}
	/**
	 * 用户许愿
	 */
	//TODO 检查数量
	public Wish makeWish(Long userid, String content, Short reply, Long typeid){
		try{
			WishType type = (WishType) wishDao.get(WishType.class, typeid);
			Wish wish = new Wish();
			wish.setContent(content);
			wish.setCreateTime(new Date());
			wish.setEnabled(ConstantDB.AVALIABLE);
			wish.setImage(null);
			wish.setReply(reply);
			wish.setTips(WishUtil.getWishTips(content));
			wish.setType(type);
			wish.setUserid(userid);
			wish = (Wish) wishDao.saveObject(wish);
			return wish;
		}catch(Exception e){
			log.error(e);
		}
		return null;
	}
	
	/**
	 * userid给wishid回复
	 */
	public WishReply saveWishReply(Long userid, Long wishid, String content){
		WishReply reply = null;
		try{
			content = FilterCharsUtil.replaceAllFilterChars(content, null);
			Wish wish = (Wish) wishDao.get(Wish.class, wishid);
			reply = new WishReply();
			reply.setContent(content);
			reply.setCreateTime(new Date());
			reply.setEnabled(ConstantDB.AVALIABLE);
			reply.setUserid(userid);
			reply.setWish(wish);
			reply = (WishReply) wishDao.saveObject(reply);
		}catch(Exception e){
			log.error(e);
		}
		return reply;
	}
	/**
	 * 删除回复
	 */
	public WishReply removeWishReply(Long userid, Long replyid){
		WishReply reply = null;
		try{
			reply = (WishReply) wishDao.get(WishReply.class, replyid);
			if(reply.getWish().getUserid().compareTo(userid) == 0){
				wishDao.remove(WishReply.class, reply.getReplyid());
			}
		}catch(Exception e){
			log.error(e);
		}
		return reply;
	}
	/**
	 * 获得第一个心愿类型，通过ID 升序
	 */
	public WishType getTheFirstWishType(){
		PaginationSupport page = new PaginationSupport();
		page.setAutoCount(false);
		page.setPageSize(1);
		page.setClazz(WishType.class);
		page.setOrderBy("typeid");
		page.setOrder("ASC");
		getPagination(page);
		List list = page.getItems();
		if(list != null && list.size() > 0){
			return (WishType) list.get(0);
		}
		return null;
	}
	/**
	 * 获得所有心愿类型
	 */
	public void getWishTypes(PaginationSupport page){
		page.setClazz(WishType.class);
		page.setOrderBy("createTime");
		page.setOrder("ASC");
		getPagination(page);
	}
	/**
	 * 获得心愿类型<br/>
	 * enabled = ConstantDB.AVALIABLE / ConstantDB.UNAVALIBLE<br/>
	 * enabled == null 取全部
	 */
	public List<WishType> getWishTypes(Short enabled){
		try {
			return wishDao.getWishTypes(enabled);
		} catch (Exception e) {
			log.error(e);
			return new ArrayList<WishType>();
		}
	}
	
	/**
	 * 通过WISHID获得回复，分页;页码：pageIndex，页大小：pageSize
	 */
	public List<WishReply> getWishReplies(Long wishid, int pageIndex, int pageSize){
		String queryString = "from WishReply where wish.wishid="+wishid +" and enabled=" + ConstantDB.AVALIABLE + " order by replyid desc";
		PaginationSupport page = new PaginationSupport();
		page.setPageIndex(pageIndex);
		page.setPageSize(pageSize);
		page.setQueryString(queryString);
//		page.setOrder("ASC");
//		page.setOrderBy("replyid");
		page.setAutoCount(false);
		getPagination(page);
		return page.getItems();
	}
	/**
	 * 通过类型获得类型下的心愿
	 */
	public void getWishes(PaginationSupport page, Long typeid){
		String queryString = "from Wish where enabled=" + ConstantDB.AVALIABLE + " and type.typeid="+typeid + " order by createTime";
		page.setQueryString(queryString);
		getPagination(page);
	}
	
	/**
	 * 通过类型获得类型下的心愿,排除wishid
	 */
	public void getWishes(PaginationSupport page, Long typeid, Long wishid){
		String queryString = "from Wish where enabled=" + ConstantDB.AVALIABLE + " and type.typeid="+typeid + " and wishid<>" + wishid + " order by createTime";
		page.setQueryString(queryString);
		getPagination(page);
	}
}
